<?php
class StoreProduct_model extends CI_Model {
    public $tabName;

    public function __construct()
    {
        $this->tabName = "kfsy_channel_store.kf_store_product";
    }
    /*
        获取本地药品数量
        params
            store_id 店铺id
    */
    public function getProductCount($store_id = 0)
    {
        $sql = "select 
                    count(*) cnt 
                from 
                    " . $this->tabName . " 
                where 
                    fk_store_id = " . $store_id;

        $countInfo = $this->db
            ->query($sql)
            ->row_array();

        return $countInfo['cnt'];
    }

    /*
        获取本地药品列表
        params
            store_id 店铺id
    */
    public function getProductList($store_id = 0)
    {
        $productId = $this->getProductIdList($store_id);

        if(empty($productId)){
            return array();
        }

        $this->load->model("Product_model", "ProductModel");
        $ProductModel = $this->ProductModel;

        $productName = $ProductModel->getProductNameById($productId);

        $res = array();

        foreach ($productId as $key => $value) {
            $res[] = array(
                "product_id" => $value,
                "product_name" => $productName[$value]
            );
        }
        return $res;
    }

    /*
        获取本地药品id列表
        params
            store_id 店铺id
    */
    public function getProductIdList($store_id = 0)
    {
        $sql = "select 
                    fk_product_id
                from 
                    " . $this->tabName . " 
                where 
                    fk_store_id = " . $store_id . "
                order by id desc";

        $productId = $this->db
            ->query($sql)
            ->result_array();


        $res = array();

        foreach ($productId as $key => $value) {
            $res[] = $value['fk_product_id'];
        }

        return $res;
    }

    /*
        克隆药品列表
        params
            soruce_id 源药店id
            target_id 目标药店id
    */
    public function cloneProduct($soruce_id = 0, $target_id = 0)
    {
        $sourceProductList = $this->getProductIdList($soruce_id);
        $targetProductList = $this->getProductIdList($target_id);

        $newProductList = array_diff($sourceProductList, $targetProductList);

        if(empty($newProductList)){
            return false;
        }

        return $this->addProduct($target_id, $newProductList);
    }

    /*
        追加药品信息
        params
            store_id 药店id
            product_id[] 商品id数组
    */
    public function appendProduct($store_id = 0, $product_id = array())
    {
        $stockProduct = $this->getProductIdList($store_id);

        $product = array_diff($product_id, $stockProduct);

        if(empty($product)){
            return false;
        }

        return $this->addProduct($store_id, $product);
    }

    /*
        添加药品信息
        params
            store_id 店铺id
            product_id[] 药品id数组列表
    */
    public function addProduct($store_id = 0, $product_id = array())
    {
        $data = array();

        $batchCnt = 100;

        $time = date("Y-m-d H:i:s");

        foreach ($product_id as $key => $value) {
            $data[] = array(
                "fk_store_id" => $store_id,
                "fk_product_id" => $value,
                "createtime" => $time
            );

            //分批处理数据
            if(count($data) > $batchCnt){
                $this->db->insert_batch($this->tabName, $data);
                $data = array();
            }
        }

        if(empty($data) == false){
            $this->db->insert_batch($this->tabName, $data);
        }
        
        
        return true;
    }

    /*
        删除药品信息
        params
            store_id 店铺id
            product_id 药品id
    */
    public function removeProduct($store_id = 0, $product_id = 0)
    {
        $where = array(
            "fk_store_id" => $store_id,
            "fk_product_id" => $product_id,
        );

        $res = $this->db->delete($this->tabName, $where);

        return $res;
    }
}